package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Team;
import com.kkb.service.TeamService;
import com.kkb.vo.QueryVO;
import com.kkb.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("team")
@ResponseBody
public class TeamController {

    @Resource
    private TeamService teamService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO<Team> queryByPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize, QueryVO vo) {
        PageInfo<Team> info = teamService.queryByPage(pageNum, pageSize, vo);
        return new ResultVO<>(info);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVO<Team> addTeam(Team team) {
        int i = teamService.addTeam(team);
        if (i == 1) {
            return new ResultVO<Team>();
        }
        return new ResultVO<Team>(500, "服务器内部异常，请稍后再试！");
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVO<Team> findById(@PathVariable("id") int teamId) {
        Team team = teamService.findById(teamId);
        return new ResultVO<>(team);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public ResultVO<Team> updateTeam(@PathVariable("id") int teamId, Team team) {
        team.setTeamId(teamId);
        int i = teamService.updateTeam(team);
        if (i == 1) {
            return new ResultVO<Team>();
        }
        return new ResultVO<Team>(500, "服务器内部异常，请稍后再试！");
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public ResultVO<Team> deleteTeam(@PathVariable("id") int teamId) {
        int i = teamService.deleteTeam(teamId);
        if (i == 1) {
            return new ResultVO<Team>();
        }
        return new ResultVO<Team>(500, "服务器内部异常，请稍后再试！");
    }

    @RequestMapping("{id}")
    public ResultVO<Team> uploadLogo(@RequestParam("logo") MultipartFile myFile, HttpServletRequest request,
                                     @PathVariable("id") int teamId) {
        String path = request.getServletContext().getRealPath("img/uploadFile/");
        String originalFilename = myFile.getOriginalFilename();
        String randomName = UUID.randomUUID().toString().replace("-", "");
        int index = originalFilename.lastIndexOf(".");
        String hz = originalFilename.substring(index);
        String name = randomName + hz;
        try {
            myFile.transferTo(new File(path + "/" + name));
            Team team = new Team();
            team.setTeamId(teamId);
            team.setTeamLogo(name);
            int i = teamService.updateTeam(team);
            if (i == 1) {
                return new ResultVO<Team>();
            } else {
                return new ResultVO<Team>(500, "服务器内部异常，请稍后再试！");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultVO<Team>(500, "图片上传出现went！" + e.getMessage());
        }
    }
}
